import { createSlice,createAsyncThunk} from "@reduxjs/toolkit";

const fetchUser = createAsyncThunk('user/fetchUser',async (userId) => {
    const response = await fetch('./toolSlice.js');
    return await response.json();
})

console.log(fetchUser(),'fetchUser')

//切片
const toolSlice = createSlice({
    name: "voteSlice",
    initialState: {
        upNum:0,
        downNum:0,
        requestState:{
            loading:'idle',
            error:null,
            data:null
        }
    },
    reducers: {
        upNumIncrement: (state, action) => {
            state.upNum = state.upNum+action.payload;
        },
        upNumDecrement: (state, action) => {
            state.downNum = state.downNum-action.payload;
        },
        upNumReset: (state, action) => {
            state.upNum = 0;
        },
        downNumIncrement: (state, action) => {
          state.downNum = state.downNum+action.payload;
        },
        downNumDecrement: (state, action) => {
            state.downNum = state.downNum-action.payload;
        },
        downNumReset: (state, action) => {
            state.downNum = 0;
        }
    },
    extraReducers: (builder) => {
        builder.addCase(fetchUser.pending, (state, action) => {
            state.requestState.loading = 'loading';
        })
            .addCase(fetchUser.fulfilled, (state, action) => {
                state.requestState.loading = 'success';
                state.requestState.data = action.payload;
            })
            .addCase(fetchUser.rejected, (state, action) => {
                state.requestState.loading = 'error';
                state.requestState.error = action.error.message;
            })
        ;
    }
});
export const {upNumIncrement, downNumIncrement} = toolSlice.actions;
export default toolSlice.reducer;